var app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
		swiperList: [],
		limit:5,
		newslist:[],
    twoNews:[],
    threeNews:[],
		searchvalue:'',
		swiper: [
			{
				id: 1,
				photourl: '../../static/banner/banner1.jpg'
			},
			{
				id: 1,
				photourl: '../../static/banner/banner2.jpg'
			}
		],
  },

	searchlist(e) {
		this.setData({
			searchvalue: e.detail.value
		})
	},
	searchdesc(){
		var that = this;
		var value = that.data.searchvalue;
		wx.showLoading({
			title: '搜索中',
		})
		if(value == ''){
			wx.showToast({
				title: '请输入搜索内容',
				duration:1000,
				icon:'none'
			})
		}else{
			var setid = app.globalData.cityCode;
			
			app.core('get', 'search_zx_by_tl/site_id/' + setid +'/limit/5',{
				keywords: value
			},(res=>{
				console.log(res)
				if(res.data.length == 0){
					wx.showToast({
						title: '暂无相关资讯',
					duration:1000,
					icon:'none'
					})
				}else{
						wx.navigateTo({
							url: './zixunsearch?keyword='+value+'',
						})
				}
				wx.hideLoading()
			}))
		}
	},
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		this.guanggao();
		this.list(5)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
	guanggao() {
		var that = this;
		var setid = app.globalData.cityCode;
    wx.showLoading({
      title: '正在加载',
    })
		app.core('get', 'get_advert/site_id/' + setid +'', '', (res) => {
			console.log(res)
			that.setData({
				swiperList: res.data
			});
      wx.hideLoading()
		})
	},
	list(num){
    wx.showLoading({
      title: '正在加载',
    })
	var that = this;
	var n = that.data.limit;
	var list =that.data.newslist;
		var setid = app.globalData.cityCode;
		app.core('get', 'get_hot_news/site_id/' + setid +'/limit/' + num+'',"",(res=>{
			console.log(res)
      that.setData({
        twoNews: res.ztc,
        threeNews:res.dtp
      })
			var arrs = res.data;
			for(var i =0; i<arrs.length;i++){
				var time = that.getLocalTime(arrs[i].createTime )
				arrs[i].createTime = time;
				list.push(arrs[i])
			}
			
			var x = list;
			var y =[];
			for(var i=0;i<x.length;i++){
				if(i<2){
					y.push(x[i])
				}
			}
			that.setData({
				newslist:y,
				limit: num+5,
			})
      wx.hideLoading()

		}))

	},
	listmore(limit){
    wx.showLoading({
      title: '正在加载',
    })
		var that = this;
		var num = that.data.limit;
		var setid = app.globalData.cityCode;
		app.core('get', 'get_hot_news/site_id/' + setid + '/limit/' + num + '', "", (res => {
			console.log(res)
			that.setData({
			
				threeNews: res.data
			})
		
			that.setData({
			
				limit: num + 5,
			})

		}))
    wx.hideLoading()
	},


	//获取资讯列表
  getLocalTime(date) {
		// return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
   var date = new Date(date * 1000);//如果date为13位不需要乘1000
   var Y = date.getFullYear() + '-';
   var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
   var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
   var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
   var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
   var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
   return Y + M + D + h + m + s;


	}, // 转换时间戳
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(111)
		var that = this;
		var num = that.data.limit;
		console.log(num);
	
		that.listmore(num)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})